﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignPatternDemoCApp.Structured.FlyweightPattern
{
    /// <summary>
    /// 该类型相当于是享元的工厂---相当于FlyweightFactory类型
    /// </summary>
    public sealed class SoldierFactory
    {
        private static IList<Soldier> soldiers;

        static SoldierFactory()
        {
            soldiers = new List<Soldier>();
        }

        Soldier mySoldier = null;
        //因为我这里有两种士兵，所以在这里可以增加另外一个参数，士兵类型，原模式里面没有，
        public Soldier GetSoldier(string name, Weapen weapen, SoldierType soldierType)
        {
            foreach (Soldier soldier in soldiers)
            {
                if (string.Compare(soldier.Name, name, true) == 0)
                {
                    mySoldier = soldier;
                    return mySoldier;
                }
            }
            //我们这里就任务名称是唯一的
            if (soldierType == SoldierType.Normal)
            {
                mySoldier = new NormalSoldier(name);
            }
            else
            {
                mySoldier = new WaterSoldier(name);
            }
            mySoldier.WeapenInstance = weapen;

            soldiers.Add(mySoldier);
            return mySoldier;
        }
    }
}
